package com.beemans.corekit.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.beemans.corekit.constant.AdvertConstants;
import com.beemans.corekit.databinding.FragmentAdmobBannerBinding;
import com.beemans.corekit.integration.admob.ATAdInfoExt;
import com.beemans.corekit.integration.admob.AbAdvertManager;
import com.beemans.corekit.integration.admob.AbBannerAdvert;
import com.beemans.corekit.integration.admob.AbBannerAdvertCallback;
import com.beemans.corekit.integration.admob.AbBannerAdvertConfig;
import com.beemans.corekit.integration.toolkit.ToastUtils;
import com.beemans.corekit.ui.dialog.LoadingDialog;
import com.google.android.gms.ads.AdValue;
import com.google.android.gms.ads.ResponseInfo;

public class AdmobBannerFragment extends BaseFragment<FragmentAdmobBannerBinding> {
    @Nullable
    @Override
    protected FragmentAdmobBannerBinding getViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return FragmentAdmobBannerBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected void initEvents() {
        binding.bannerBtnLoadAd.setOnClickListener(v -> showAdvert());
    }

    private void showAdvert() {
        LoadingDialog.showLoading();

        final AbBannerAdvertConfig advertConfig = new AbBannerAdvertConfig(AdvertConstants.AB_BANNER_ID);
        advertConfig.setCollapsible(false);

        AbAdvertManager.loadBannerAdvert(this, binding.bannerFlAd, advertConfig, new AbBannerAdvertCallback() {
            @Override
            public void onAdvertRequestPre(@NonNull AbBannerAdvert abBannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告开始请求");
            }

            @Override
            public void onAdvertRequestAlt(@NonNull AbBannerAdvert abBannerAdvert, @NonNull ATAdInfoExt adInfoExt) {
                printLog("广告请求成功");
            }

            @Override
            public void onAdvertLoaded(@NonNull AbBannerAdvert abBannerAdvert) {
                printLog("广告加载成功");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertLoadFail(@NonNull AbBannerAdvert abBannerAdvert, @Nullable String adError) {
                printLog("广告加载失败");
                LoadingDialog.hideLoading();
            }

            @Override
            public void onAdvertShow(@NonNull AbBannerAdvert abBannerAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告展示 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertShowFail(@NonNull AbBannerAdvert abBannerAdvert) {
                printLog("广告展示失败");
            }

            @Override
            public void onAdvertClicked(@NonNull AbBannerAdvert abBannerAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告点击 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertClose(@NonNull AbBannerAdvert abBannerAdvert, @Nullable ResponseInfo responseInfo) {
                printLog("广告关闭 --- responseInfo:" + responseInfo);
            }

            @Override
            public void onAdvertRevenue(@NonNull AbBannerAdvert abBannerAdvert, @Nullable ResponseInfo responseInfo, @NonNull AdValue adValue) {
                printLog("广告收益 --- getValueMicros:" + adValue.getValueMicros());
                printLog("广告收益精度 --- getPrecisionType:" + adValue.getPrecisionType());
                printLog("广告收益货币代码 --- getCurrencyCode:" + adValue.getCurrencyCode());
            }
        });
    }

    private void printLog(String msg) {
        Log.e("susu", msg);
        ToastUtils.showShort(msg);
    }
}